package com.jshoperxms.controller.api.introsite;

import com.jshoperxms.Response;
import com.jshoperxms.controller.api.introsite.vo.IntroIndexVo;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.entity.CommunityCircleArticleCommentT;
import com.jshoperxms.entity.CommunityCircleArticleT;
import com.jshoperxms.entity.CommunityCircleT;
import com.jshoperxms.service.CommunityCircleArticleCommentTService;
import com.jshoperxms.service.CommunityCircleArticleTService;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sdywcd on 16/12/5.
 * Des:
 */
@Controller
@RequestMapping("/api/mall/introsite")
public class IntroSiteController extends BaseTController {

    @Resource
    private CommunityCircleArticleTService communityCircleArticleTService;

    @Resource
    private CommunityCircleArticleCommentTService communityCircleArticleCommentTService;

    private Map<String,Object> responsedata=new HashMap<>();




    @RequestMapping(value="/findAll",method= RequestMethod.POST)
    public ResponseEntity findAll(@RequestBody SearchModel searchModel){
        List<IntroIndexVo> lists=new ArrayList<>();
        Criterion criterion= Restrictions.and(Restrictions.eq("communitycirclename",searchModel.getName())).add(Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState()));
        List<CommunityCircleArticleT> list=this.communityCircleArticleTService.findByCriteria(CommunityCircleArticleT.class,criterion, Order.desc(TagWords.UPDATETIME));
        if(!list.isEmpty()){
            for(CommunityCircleArticleT c:list){
                IntroIndexVo introIndexVo=new IntroIndexVo();
                introIndexVo.setCircleArticleT(c);
                //查询评论
                Criterion criterion1=Restrictions.and(Restrictions.eq("communitycirclearticleid",c.getId())).add(Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState()));
                List<CommunityCircleArticleCommentT> cbeanlist=this.communityCircleArticleCommentTService.findByCriteria(CommunityCircleArticleCommentT.class,criterion1,Order.desc(TagWords.UPDATETIME));
                if(cbeanlist!=null){
                    introIndexVo.setCircleArticleCommentTs(cbeanlist);
                }
                lists.add(introIndexVo);
            }
        }
        responsedata.put(TagWords.BEANLIST,lists);
        responsedata.put(TagWords.SUCCESS, true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        return Response.success(responsedata);
    }

}
